﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections.ObjectModel;
using System.Windows.Input;
using VisaCRM.Modules.Admin.Model;
using VisaCRM.Modules.Admin.Gateway;
using VisaCRM.Modules.Admin.ViewModels.Helper;
using Microsoft.Practices.Prism.Mvvm;

namespace VisaCRM.Modules.Admin.ViewModels
{
    /// <summary>
    /// ViewModel of an list of industries
    /// </summary>
    class IndustryViewModel:BindableBase
    {
        /// <summary>
        /// initialises all collection used
        /// </summary>
        private ObservableCollection<Industry> allIndustries;

        /// <summary>
        /// initialise gateway used
        /// </summary>
        IndustryGateway industryGateway = new IndustryGateway();

        ///summary
        ///initialises a new instance of IndustryViewmodel class
       ///summary
        public IndustryViewModel(ObservableCollection<Industry> allindustries)
       {
           currentIndustry = new Industry();
           if (allindustries == null || allindustries.Count == 0)
           {

               AllIndustries = industryGateway.GetAllindustries();
           }
           else
           {
               this.AllIndustries = allindustries;
           }

           /// Re-act to any changes from outside this ViewModel
           this.AllIndustries.CollectionChanged += (sender, e) =>
           {
               if (e.OldItems != null && e.OldItems.Contains(this.CurrentIndustry))
               {
                   this.CurrentIndustry = null;
               }
           };
           
 
           this.SaveCommand = new DelegateCommand((o) => this.Save());
           this.AddNewCommand = new DelegateCommand((o) => this.AddNew());
           this.CancelCommand = new DelegateCommand((o) => this.Cancel());
           this.DeleteCommand = new DelegateCommand((o) => this.Delete());
           this.EditCommand = new DelegateCommand((o) => this.Edit());
       }

       public ICommand AddNewCommand { get; private set; }
       public ICommand SaveCommand { get; private set; }
       public ICommand CancelCommand { get; private set; }
       public ICommand DeleteCommand { get; private set; }
       public ICommand EditCommand { get; private set; }

       private void Save()
       {
           if (this.CurrentIndustryName != null)
           {
               Industry industry = new Industry() { IndustryName = this.CurrentIndustryName };
               bool flag = false;
               if (string.IsNullOrEmpty(industry.IndustryName))
                   return;
               foreach (Industry ind in this.AllIndustries)
               {
                   if (ind.IndustryName == industry.IndustryName)
                   {
                       flag = true;
                       break;
                   }
               }
               if (flag)
               {
                   System.Windows.Forms.MessageBox.Show("The industry details are already listed");
                   return;
               }
               industryGateway.SaveNewIndustryToDatabase(this.CurrentIndustryName);
               AddNew();
               this.AllIndustries = industryGateway.GetAllindustries();
               System.Windows.Forms.MessageBox.Show("The industry details are saved sucessfully");
           }
           else
           {
               System.Windows.Forms.MessageBox.Show("Please fill industry details");
           }
       }

       private void AddNew()
       {
           this.CurrentIndustryName = null;
       }
       private void Cancel()
       {
           this.currentIndustry = null;
       }
       private void Delete()
       {
           if (this.CurrentIndustry != null)
           {
               industryGateway.DeleteindustryFromDatabase(this.currentIndustry.IndustryId);
               AllIndustries.Remove(this.CurrentIndustry);
               AddNew();
               System.Windows.Forms.MessageBox.Show("The industry details is deleted sucessfully");
           }
           else
           {
               System.Windows.Forms.MessageBox.Show("Please selsect industry details to be deleted");
           }
         
       }

       private void Edit()
       {
           if (this.CurrentIndustry != null)
           {
               Industry industry = new Industry() { IndustryName = this.CurrentIndustryName,IndustryId=this.CurrentIndustry.IndustryId };
               industryGateway.EditIndustryToDatabase(this.CurrentIndustryName, this.currentIndustry.IndustryId);
               AllIndustries = industryGateway.GetAllindustries();
               this.CurrentIndustry = industry;
               System.Windows.Forms.MessageBox.Show("The industry details is editted sucessfully");
           }
           else
           {
               System.Windows.Forms.MessageBox.Show("Please selsect industry details to be edited");
           }
       }

       /// <summary>
       /// The industry currently selected in the workspace
       /// </summary>
       private Industry  currentIndustry;

       /// <summary>
       /// Gets or sets the industry currently selected in the workspace
       /// </summary>
       public Industry CurrentIndustry
       {
           get
           {
               return this.currentIndustry;
           }

           set
           {
               this.currentIndustry = value;
               if (this.CurrentIndustry != null)
                   this.CurrentIndustryName = this.CurrentIndustry.IndustryName;
               this.OnPropertyChanged("CurrentIndustry");
           }
       }

       /// <summary>
       /// the IndustryName currently selected in the workspace
       /// </summary>
       private string currentIndustryName;
       /// <summary>
       /// Gets or sets the IndustryName currently selected in the workspace
       /// </summary>
       public string CurrentIndustryName
       {
           get
           {
               return this.currentIndustryName;
           }

           set
           {
               this.currentIndustryName = value;
               this.OnPropertyChanged("CurrentIndustryName");
           }
       }
       
       /// <summary>
       /// Gets or sets the industry collection
       /// </summary>
       public ObservableCollection<Industry> AllIndustries
       {
           get
           {
               return this.allIndustries;
           }

           set
           {
               this.allIndustries = value;
               this.OnPropertyChanged("AllIndustries");
           }
       }
    }
}
